home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: passing 2D arrays to functions
- Date: 18 Apr 1996 17:22:59 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4l5tpj$1eu@sparcserver.lrz-muenchen.de>
- References: <4l58sk$l6r@harbinger.cc.monash.edu.au>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- pdrod1@mdw084.cc.monash.edu.au (Mr Paul Rodger) writes:
-
- >Greets -
-
- >I need to pass a 2-dimensional array to a function. Obviously I just want
- >to pass a pointer to the first element, and in the past when I wanted to
- >pass a one-dimensional array to a function I did the following which should
- >work fine:
-
- >fun(int array[])
- >{
- >...
- >}
-
- >But when I try to pass a 2D array using the same method, slightly different:
-
- >fun(int array[][])
- >{
- >...
- >}
-
- This is more than slightly different from the above. "array" is a pointer
- to int in the first example, whereas it is a pointer to an array of
- unspecified size of int (i.e. an incomplete type) in the second example.
-
- While there is nothing wrong with passing pointers to incomplete types
- in C, it is impossible to use an incomplete type to access elements or
- members of the complete type behind it.
-
- >main()
- >{
- > int array[10][10];
- >...
- > fun(array);
- >}
-
- >I'm getting errors:
-
- >main.c:221: arithmetic on pointer to an incomplete type
- >make: *** [main.o] Error 1
-
- >This occurs when I try to look at elements in the array when I am
- >in the 'fun' function:
-
- >array[1][1] = 1;
-
- >Would this be because of vagueness about row-major or column-major form?
-
- No, certainly not. It is because in C, an array of unspecified size is an
- incomplete type.
-
- >I've got around this in the past by putting the 2D array in a struct,
- >and passing a pointer to a struct, but in this case I can't do that
- >because the array size could vary as it is declared various times in
- >iterations. (It isn't declared in main like above - this is just a
- >simplication).
-
- The FAQ explains one method for handling two-dimensional dynamic arrays.
- If you need only the first dimension of the array to be dependent on
- dynamic information, try
-
- int fun(int (* array)[10]);
-
- You can pass an array of N arrays of 10 ints to "fun()", where N can
- be different in different invocations of the function. (You will have
- to pass the actual size of the array as an additional parameter).
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-